package be.xios.projectquiz.master;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import be.xios.projectquiz.cards.Connect;
import be.xios.projectquiz.cards.Loading;
import be.xios.projectquiz.global.CardsViewer;
import be.xios.projectquiz.global.CheckIP;
import be.xios.projectquiz.global.ServerListFrame;
import be.xios.projectquiz.global.SharedItems;

/// <summary>
/// Deze klasse bevat de GUI van de master
/// </summary>
/// <author>Sebastian Kubski</author>
@SuppressWarnings("serial")
public class MasterGUI extends JFrame {

	private JMenuBar menubar;
	private JMenu connectmenu;
	private JMenuItem connect, serverlist;
	private JPanel central; //CardLayout
	private JLabel connstatus;
	private SharedItems shared;
	private CardsViewer viewer;
	
	/// <summary>
	/// Deze constructor boot de GUI van de master
	/// </summary>
	/// <author>Sebastian Kubski</author>
	public MasterGUI() {
		super("MasterGUI.java");
		
		menubar = new JMenuBar();
		
		connectmenu = new JMenu("Start");
		
			connect = new JMenuItem("Connect");
			connect.addActionListener(
					new ActionListener()
					{
						public void actionPerformed( ActionEvent event ) {
							CheckIP check = new CheckIP(shared, "MASTER", viewer);
							if( check.checkConfigFile() ) {
								check.openFileRead();
							}
							else {
								check.getInput();
							} 
						}
					}			
			);
			
			serverlist = new JMenuItem("ServerList");
			serverlist.addActionListener(
					new ActionListener()
					{

						@Override
						public void actionPerformed(ActionEvent arg0) {
							ServerListFrame app = new ServerListFrame();
							app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
							app.setSize(400, 400);
							app.setLocationRelativeTo(null);
							app.setVisible(true);				
						}							
					}				
			);
		
			connectmenu.add(connect);
			connectmenu.add(serverlist);
			
			menubar.add(connectmenu);
	
		central = new JPanel(new CardLayout());
		
			//card1
			Connect connectCard = new Connect();
			//card2
			Loading loading = new Loading("Wachten tot iedereen klaar is");
		
		central.add(connectCard, "CONNECT");
		central.add(loading ,"LP");
		
		viewer = new CardsViewer(central);
		
		connstatus = new JLabel("Not connected");
		
		add(menubar, BorderLayout.NORTH);
		add(central, BorderLayout.CENTER);
		add(connstatus, BorderLayout.SOUTH);
		
		shared = new SharedItems(this, connstatus, connect);
	}
}
